package hamaster.jpbc.userconfig;

import static hamaster.jpbc.userconfig.ConfigKeys.IO_BLOCK_SIZE;
import static hamaster.jpbc.userconfig.ConfigKeys.AES_PREFIX;
import static hamaster.jpbc.userconfig.ConfigKeys.HASH_PREFIX;
import static hamaster.jpbc.userconfig.ConfigKeys.METHOD;
import static hamaster.jpbc.userconfig.ConfigKeys.OPERATION_MODE;
import static hamaster.jpbc.userconfig.ConfigKeys.RC4_PREFIX;
import static hamaster.jpbc.userconfig.ConfigKeys.SYMM_KEY_LENGTH;
import hamaster.jpbc.Main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class UserConfigManager implements Runnable {

	private static final Properties DEFAULT_PROPERTIES;

	static {
		DEFAULT_PROPERTIES = new Properties();
		DEFAULT_PROPERTIES.setProperty(AES_PREFIX + OPERATION_MODE, "CBC");
		DEFAULT_PROPERTIES.setProperty(AES_PREFIX + SYMM_KEY_LENGTH, "256");
		DEFAULT_PROPERTIES.setProperty(RC4_PREFIX + SYMM_KEY_LENGTH, "256");
		DEFAULT_PROPERTIES.setProperty(HASH_PREFIX + METHOD, "SHA1");
		DEFAULT_PROPERTIES.setProperty(IO_BLOCK_SIZE, "1024");
	}

	private Main application;

	private static String nativeDataPath;

	private static String configFolderName;

	private static String configFileName;

	static public String getConfigFileName() {
		return configFileName;
	}

	static public String getConfigFolderName() {
		return new File(new File(nativeDataPath), configFolderName).getAbsolutePath();
	}

	public UserConfigManager(Main application) {
		this.application = application;
		// TODO 判断操作系统
		if (System.getProperty("os.name").toLowerCase().contains("windows")) {
			nativeDataPath = System.getenv("APPDATA");
			configFolderName = "IBC";
			configFileName = "userdata.ini";
		} else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
			nativeDataPath = "/home/" + System.getProperty("user.name");
			configFolderName = ".IBC";
			configFileName = "userdata.properties";
		} else
			throw new RuntimeException("Unsupported OS!");
	}

	@Override
	public void run() {
		File folder = new File(new File(nativeDataPath), configFolderName);
		try {
			if (folder.exists() && folder.isDirectory()) {
				File ini = new File(folder, configFileName);
				if (ini.exists()) {
					UserConfig config = new UserConfig();
					Properties tmp = new Properties();
					FileInputStream in = new FileInputStream(ini);
					tmp.load(in);
					for (Object key : tmp.keySet()) {
						config.setProperty((String) key, tmp.getProperty((String) key));
					}
					in.close();
					application.setUserConfig(config);
				} else {
					firstRun();
				}
			} else {
				if (folder.exists())
					folder.delete();
				firstRun();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 第一次运行 新建配置文件夹和配置文件
	 * @throws IOException
	 */
	private void firstRun() throws IOException {
		File folder = new File(new File(nativeDataPath), configFolderName);
		folder.mkdir();
		File ini = new File(folder, configFileName);
		UserConfig config = new UserConfig();
		ini.createNewFile();
		FileOutputStream out = new FileOutputStream(ini);
		DEFAULT_PROPERTIES.store(out, null);
		out.close();
		for (Object key : DEFAULT_PROPERTIES.keySet()) {
			config.setProperty((String) key, DEFAULT_PROPERTIES.getProperty((String) key));
		}
		application.setUserConfig(config);
	}
}
